package com.credithc.signature.manager.controller.api;

import com.credithc.signature.common.mapper.CallStatisticsMapper;
import com.credithc.signature.common.mapper.RequestLogMapper;
import com.credithc.signature.common.po.CallStatistics;
import com.credithc.signature.common.utils.TimeUtil;
import com.credithc.signature.manager.service.StatisticsSerivce;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/scs/v2")
@Slf4j
public class CronTestController {

    @Autowired
    private CallStatisticsMapper callStatisticsMapper;
    @Autowired
    private StatisticsSerivce statisticsSerivce;
    @Autowired
    private RequestLogMapper requestLogMapper;

    @GetMapping("cron/test")
    public String test(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String timeId = format.format(new Date());
        statisticsSerivce.bulidChannelDayDate(timeId);
        log.info("执行定时任务 统计调用成功失败数量插入对应表中 每天执行一次 现在时间为"+new Date());
        Timestamp time = new Timestamp(TimeUtil.getDayEndTime(new Date()).getTime());
        Timestamp  beforeTime= new Timestamp(TimeUtil.getDayStartTime(new Date()).getTime());
        List<CallStatistics> callStatistics = new ArrayList<>();
        bulidbeforeDayDate(2,timeId, time, beforeTime, callStatistics);
        bulidbeforeDayDate(3,timeId, time, beforeTime, callStatistics);
        return "调用成功";
    }

    private void bulidbeforeDayDate(Integer status,String timeId, Timestamp time, Timestamp beforeTime, List<CallStatistics> callStatistics) {
        try {
            callStatistics = requestLogMapper.selectCountByStatusGroupByChannel(status,time,beforeTime);
        } catch (Exception e) {
            log.info("统计类CronTestController[bulidbeforeDayDate]方法发生异常", e);
        }
        if (status == 2){
            status =0;
        }else{
            status = 1;
        }
        int successNum = 0;
        for (CallStatistics c:callStatistics) {
            Example example = new Example(CallStatistics.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("timeId",timeId);
            criteria.andEqualTo("status",status);
            criteria.andEqualTo("channelNo",c.getChannelNo());
            c.setTimeId(timeId);
            c.setStatus(status);
            try {
                callStatisticsMapper.updateByExampleSelective(c,example);
            } catch (Exception e) {
                log.error("更新统计数量出错", e);
            }
            successNum += c.getNum();
        }
        CallStatistics callStatistics1 = new CallStatistics();
        callStatistics1.setTimeId(timeId);
        callStatistics1.setStatus(status);
        callStatistics1.setChannelNo("0");
        callStatistics1.setNum(successNum);
        callStatisticsMapper.insert(callStatistics1);
    }

    @GetMapping("cron/test/del")
    public String testDel(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String timeId = format.format(new Date());
        Example example = new Example(CallStatistics.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("timeId",timeId);
        try {
            callStatisticsMapper.deleteByExample(example);
        } catch (Exception e) {
            log.error("统计类CronTestController[testDel]方法发生异常", e);
            return e.getMessage();
        }
        return "删除测试数据成功";
    }
}
